package cn.seocoo.platform.api.controller;

import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.api.util.PageBean;
import cn.seocoo.platform.base.domain.BaseVehicleType;
import cn.seocoo.platform.base.dto.BaseCommonDto;
import cn.seocoo.platform.base.service.IBaseVehicleTypeService;
import cn.seocoo.platform.car.dto.CarDto;
import cn.seocoo.platform.car.service.ICarInfoService;
import cn.seocoo.platform.common.core.domain.AjaxResult;
import cn.seocoo.platform.common.utils.AmountUtils;
import cn.seocoo.platform.common.utils.StringUtils;
import cn.seocoo.platform.search.service.ISearchRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ApiSearchTitleController
 * @description: 筛选记录操作
 * @author: wujian
 * @Date: 2019-12-25 15:51
 **/

@RestController
@RequestMapping("/api/search")
public class ApiSearchController {

    @Autowired
    private ISearchRecordService iSearchRecordService;


    @Autowired
    private ICarInfoService iCarInfoService;
    @Autowired
    private IBaseVehicleTypeService iBaseVehicleTypeService;

    /**
     * @Author WuJian
     * @Description //查询用户的搜索记录
     * @Date 16:25 2019/12/25
     * @Param [appUserId]
     * @return cn.seocoo.platform.common.core.domain.AjaxResult
     **/
    @GetMapping("/getSearcRecord")
    public JSONResult getSearcRecord(Integer appUserId){
        List<BaseCommonDto> list = iSearchRecordService.selectSearchRecordByAppUserId(appUserId);
        return JSONResult.ok(list);

    }


    /**
     * @Author WuJian
     * @Description //清空用户的搜索记录
     * @Date 16:25 2019/12/25
     * @Param [appUserId]
     * @return cn.seocoo.platform.common.core.domain.AjaxResult
     **/
    @PostMapping("/deleteSearchRecord")
    public JSONResult deleteSearchRecord(@RequestBody ModelMap modelMap){
        Object appUserId = modelMap.get("appUserId");
        if(StringUtils.isNull(appUserId)){
            return JSONResult.errorMsg(JSONResult.NOT_FOUND_USER);
        }else{
            iSearchRecordService.deleteSearchRecordByAppUserId(Integer.valueOf(appUserId.toString()));
            return JSONResult.ok(JSONResult.DELETE_MSG);
        }
    }



    /**
     * @return cn.seocoo.platform.common.core.domain.JSONResult
     * @Author WuJian
     * @Description // 首页顶部搜索
     * @Date 11:48 2019/12/25
     * @Param []
     **/
    @GetMapping("/getCarList")
    public JSONResult getCarList(String title, Integer pageNum, Integer pageSize,Integer appUserId) {
        if(appUserId == null){
            return JSONResult.errorMsg("当前用户不存在");
        }
        List<CarDto> carlist = iCarInfoService.getCarList(title, pageNum, pageSize,appUserId);
        PageBean<CarDto> carDtoPageInfo = new PageBean<>(carlist);
        List<CarDto> list = carDtoPageInfo.getList();
        if (StringUtils.isNotEmpty(list)) {
            for (CarDto carDto : list) {
                if (StringUtils.isNotBlank(carDto.getBaseCarriageSize())) {
                    Integer vehicleId = carDto.getVehicleId();
                    BaseVehicleType baseVehicleType = iBaseVehicleTypeService.selectBaseVehicleTypeById(vehicleId);
                    carDto.setBaseCarriageSize(carDto.getBaseCarriageSize() + baseVehicleType.getDimension());
                }
                if(StringUtils.isNotBlank(carDto.getPicPath())){
                    carDto.setPicPath(carDto.getPicPath().split(",")[0]);
                }
                if (StringUtils.isNotBlank(carDto.getAmount())) {
                    carDto.setAmount(AmountUtils.changeF2W(carDto.getAmount()) + "万");
                }
                if (StringUtils.isNotBlank(carDto.getArrivalTime())) {
                    carDto.setArrivalTime(carDto.getArrivalTime().substring(2, 4) + "年" + carDto.getArrivalTime().substring(5, 7) + "月");
                }

            }
        }


        return JSONResult.ok(carDtoPageInfo);
    }
}
